package com.zhaoyang.servlet;

import com.zhaoyang.dao.ClazzDao;
import com.zhaoyang.model.Clazz;
import com.zhaoyang.model.Page;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 赵扬
 * @time 2020/4/28 12:58
 */
@WebServlet("/clazzServlet")
public class ClazzServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getParameter("method");

        if ("toClazzListView".equals(method)) {
            clazzList(request, response);
        } else if ("getClazzList".equals(method)) {
            getClazzList(request, response);
        } else if ("AddClazz".equals(method)) {
            addClazz(request, response);
        } else if ("DeleteClazz".equals(method)) {
            deleteClazz(request, response);
        } else if ("EditClazz".equals(method)) {
            editClazz(request, response);
        }
    }

    /**
     * 班级班级信息
     *
     * @param request
     * @param response
     */
    private void editClazz(HttpServletRequest request,
                           HttpServletResponse response) {

        Integer id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String info = request.getParameter("info");

        Clazz clazz = new Clazz();
        clazz.setName(name);
        clazz.setInfo(info);
        clazz.setId(id);

        ClazzDao clazzDao = new ClazzDao();
        if (clazzDao.editClazz(clazz)) {
            try {
                response.getWriter().write("success");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 删除班级
     *
     * @param request
     * @param response
     */
    private void deleteClazz(HttpServletRequest request,
                             HttpServletResponse response) {
        Integer id = Integer.parseInt(request.getParameter("clazzid"));
        ClazzDao clazzDao = new ClazzDao();
        if (clazzDao.deleteClazz(id)) {
            try {
                response.getWriter().write("success");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 添加班级
     *
     * @param request
     * @param response
     */
    private void addClazz(HttpServletRequest request,
                          HttpServletResponse response) {
        String name = request.getParameter("name");
        String info = request.getParameter("info");
        Clazz clazz = new Clazz();
        clazz.setName(name);
        clazz.setInfo(info);
        ClazzDao clazzDao = new ClazzDao();
        if (clazzDao.addClazz(clazz)) {
            try {
                response.getWriter().write("success");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void clazzList(HttpServletRequest request,
                           HttpServletResponse response) {
        try {
            request.getRequestDispatcher("view/clazzList.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void getClazzList(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("clazzName");
        Integer currentPage = request.getParameter("page") == null ? 1 : Integer.parseInt(request.getParameter("page"));
        Integer pageSize = request.getParameter("rows") == null ? 999 : Integer.parseInt(request.getParameter("rows"));

        Clazz clazz = new Clazz();
        clazz.setName(name);

        ClazzDao clazzDao = new ClazzDao();
        List<Clazz> clazzList = clazzDao.getClazzList(clazz, new Page(currentPage, pageSize));
        long total = clazzDao.getClazzListTotal(clazz);
        response.setCharacterEncoding("UTF-8");
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", total);
        result.put("rows", clazzList);

        try {
            String from = request.getParameter("from");
            if ("combox".equals(from)) {
                response.getWriter().write(JSONArray.fromObject(clazzList).toString());
            } else {
                response.getWriter().write(JSONObject.fromObject(result).toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
